package com.nhjf.mobile.controller.postJob;


import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.nhjf.command.comment.OperationCommand;
import com.nhjf.command.jobDetails.JobDetailsCommand;
import com.nhjf.common.page.Pagination;
import com.nhjf.mobile.common.util.ImageTools;
import com.nhjf.mobile.common.util.SysProperties;
import com.nhjf.model.jobDetails.JobDetails;
import com.nhjf.model.postJob.PostJob;
import com.nhjf.model.student.Student;
import com.nhjf.model.system.AdminSession;
import com.nhjf.model.system.User;
import com.nhjf.model.teachers.Teacher;
import com.nhjf.pojo.dto.result.ServiceResultDto;
import com.nhjf.pojo.qo.jobDetails.JobDetailsQo;
import com.nhjf.pojo.qo.postJob.PostJobQo;
import com.nhjf.pojo.qo.studnet.StudentQo;
import com.nhjf.pojo.qo.system.AdminSessionQo;
import com.nhjf.pojo.qo.system.UserQo;
import com.nhjf.pojo.qo.teachers.TeachersQo;
import com.nhjf.service.jobDetails.JobDetailsService;
import com.nhjf.service.postJob.PostJobService;
import com.nhjf.service.student.StudentService;
import com.nhjf.service.system.AdminSessionService;
import com.nhjf.service.system.UserService;
import com.nhjf.service.teachers.TeachersService;

import net.sf.json.JSONObject;

@RequestMapping("/postJob")
@Controller
public class PostJobController {
	@Autowired
	private PostJobService jobService;
	@Autowired
	private JobDetailsService jobDetailsService;
	@Autowired
	private AdminSessionService adminSessionService;
	@Autowired
	private UserService userService;
	@Autowired
	private TeachersService teacherService;
	@Autowired
	private JobDetailsService detailsService;
	@Autowired
	private StudentService studentservice;
	//发布作业
	@RequestMapping("/postJob_save")
	@ResponseBody
	public String postJob_save(OperationCommand command,@RequestParam(value="token", required=true)String token) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession==null) {
				resMap.put("code", "406");
				resMap.put("message", "不存在此账号！");
				return JSONObject.fromObject(resMap).toString();
			}else {
				UserQo userQo=new UserQo();
				userQo.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo);
				if(user==null) {
					resMap.put("code", "406");
					resMap.put("message", "未登录!");
					return JSONObject.fromObject(resMap).toString();
				}else {
					TeachersQo qo=new TeachersQo();
					qo.setSeachUser(true);
					qo.setUserId(user.getId());
					qo.setSeachOrganization(true);
					Teacher teacher= teacherService.queryUnique(qo);
					if(teacher==null){
						resMap.put("code", "406");
						resMap.put("message", "不存在此账号！");
						return JSONObject.fromObject(resMap).toString();
					}
					command.setCreatUser(teacher.getId());
					command.setSetOrganizationId(teacher.getTraininginstitution().getId());
					SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					String datejr2= command.getStartTime()+" 00:00:00";
					String datejr1= command.getEndTime()+" 23:59:59";
					command.setEndDate(format1.parse(datejr1));
					command.setStartDate(format1.parse(datejr2));
					ServiceResultDto dto= jobService.createCommand(command);
					if(dto.getSuccess()) {
						resMap.put("code", "200");
						resMap.put("message","发布成功!");
						return JSONObject.fromObject(resMap).toString();
					}else {
						resMap.put("code", "500");
						resMap.put("message","发布失败!");
						return JSONObject.fromObject(resMap).toString();
					}
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message","系统错误！");
			return JSONObject.fromObject(resMap).toString();
		}
	}
	//作业列表
	@ResponseBody
	@RequestMapping(value = "/jobList")
	public String jobList(HttpServletRequest request, Model model,@RequestParam(value="token", required=true) String token,@RequestParam(value="page", defaultValue="1")Integer page){
		Map<String, Object> resMap=new HashMap<String, Object>();
		if(StringUtils.isEmpty(token)) {
			resMap.put("code", "500");
			resMap.put("message", "未登录!");
			return JSONObject.fromObject(resMap).toString();
		}
		AdminSessionQo adminSessionQo=new AdminSessionQo();
		adminSessionQo.setToken(token);
		AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
		if(adminSession==null) {
			resMap.put("code", "406");
			resMap.put("message", "不存在此账号！");
			return JSONObject.fromObject(resMap).toString();
		}else {
			adminSession.getLoginName();
			UserQo userQo=new UserQo();
			userQo.setUserName(adminSession.getLoginName());
			User user= userService.queryUnique(userQo);
			if(user==null) {

				resMap.put("code", "406");
				resMap.put("message", "未登录!");
				return JSONObject.fromObject(resMap).toString();
			}else {
				Pagination pagination = new Pagination();
				pagination.setPageSize(10);
				PostJobQo jobQo=new PostJobQo();
				TeachersQo Tqo=new TeachersQo();
				Tqo.setUserId(user.getId());
				Tqo.setSeachOrganization(true);
				Teacher teacher= teacherService.queryUnique(Tqo);
				if(teacher==null){
					resMap.put("code", "406");
					resMap.put("message", "不存在此账号！");
					return JSONObject.fromObject(resMap).toString();
				}
				jobQo.setOrganizationId(teacher.getTraininginstitution().getId());
				pagination.setCondition(jobQo);
				pagination.setPageNo(page);
				pagination = jobService.queryPagination(pagination);
				List<PostJob> jobs=(List<PostJob>)pagination.getList();
				SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				for(PostJob job:jobs){
					job.setCreateDate(format1.format(job.getCreatTime()));
					JobDetailsQo qo=new JobDetailsQo();
					qo.setPostJobId(job.getId());
					qo.setStatus(1);
					Integer submitSum=jobDetailsService.queryCount(qo);
					job.setSubmitNum(submitSum);
				}
				resMap.put("code", "200");
				resMap.put("message", pagination);
				return JSONObject.fromObject(resMap).toString();
			}
		}


	}

	//已交作业列表
	@ResponseBody
	@RequestMapping(value = "/submitList")
	public String submitList(HttpServletRequest request, Model model,@RequestParam(value="token", required=true) String token,@RequestParam(value="page", defaultValue="1")Integer page,@RequestParam(value="jobId", required=true) String jobId){
		Map<String, Object> resMap=new HashMap<String, Object>();
		if(StringUtils.isEmpty(token)) {
			resMap.put("code", "500");
			resMap.put("message", "未登录!");
			return JSONObject.fromObject(resMap).toString();
		}
		AdminSessionQo adminSessionQo=new AdminSessionQo();
		adminSessionQo.setToken(token);
		AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
		if(adminSession==null) {
			resMap.put("code", "406");
			resMap.put("message", "不存在此账号！");
			return JSONObject.fromObject(resMap).toString();
		}else {
			adminSession.getLoginName();
			UserQo userQo=new UserQo();
			userQo.setUserName(adminSession.getLoginName());
			User user= userService.queryUnique(userQo);
			if(user==null) {

				resMap.put("code", "406");
				resMap.put("message", "未登录!");
				return JSONObject.fromObject(resMap).toString();
			}else {
				Pagination pagination = new Pagination();
				pagination.setPageSize(10);
				JobDetailsQo detailsQo=new JobDetailsQo();
				detailsQo.setPostJobId(Long.parseLong(jobId));
				detailsQo.setStatus(1);
				detailsQo.setSeachStudent(true);
				pagination.setCondition(detailsQo);
				pagination.setPageNo(page);
				pagination = jobDetailsService.queryPagination(pagination);
				List<JobDetails> jobDetailsList=(List<JobDetails>)pagination.getList();
				SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				//				for(PostJob job:jobs){
				//					job.setCreateDate(format1.format(job.getCreatTime()));
				//					JobDetailsQo qo=new JobDetailsQo();
				//					qo.setPostJobId(job.getId());
				//					qo.setStatus(1);
				//					Integer submitSum=jobDetailsService.queryCount(qo);
				//					job.setSubmitNum(submitSum);
				//				}
				SysProperties sysProperties=SysProperties.getInstance();
				String operation=sysProperties.get("imagePath");
				for(JobDetails details:jobDetailsList){
					if(details.getNotchangedURL()!=null){
						details.setNotchangedURL(operation+"/operation/"+details.getNotchangedURL());
					}
					if(details.getChangepaintingURL()!=null){
						details.setChangepaintingURL(operation+"/operation/"+details.getChangepaintingURL());
					}
				}
				resMap.put("code", "200");
				resMap.put("message", pagination);
				return JSONObject.fromObject(resMap).toString();
			}
		}
	}
	//已批改交作业列表
	@ResponseBody
	@RequestMapping(value = "/correctList")
	public String correctList(HttpServletRequest request, Model model,@RequestParam(value="token", required=true) String token,@RequestParam(value="page", defaultValue="1")Integer page,@RequestParam(value="jobId", required=true) String jobId){
		Map<String, Object> resMap=new HashMap<String, Object>();
		if(StringUtils.isEmpty(token)) {
			resMap.put("code", "500");
			resMap.put("message", "未登录!");
			return JSONObject.fromObject(resMap).toString();
		}
		AdminSessionQo adminSessionQo=new AdminSessionQo();
		adminSessionQo.setToken(token);
		AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
		if(adminSession==null) {
			resMap.put("code", "406");
			resMap.put("message", "不存在此账号！");
			return JSONObject.fromObject(resMap).toString();
		}else {
			adminSession.getLoginName();
			UserQo userQo=new UserQo();
			userQo.setUserName(adminSession.getLoginName());
			User user= userService.queryUnique(userQo);
			if(user==null) {

				resMap.put("code", "406");
				resMap.put("message", "未登录!");
				return JSONObject.fromObject(resMap).toString();
			}else {
				Pagination pagination = new Pagination();
				pagination.setPageSize(10);
				JobDetailsQo detailsQo=new JobDetailsQo();
				detailsQo.setPostJobId(Long.parseLong(jobId));
				detailsQo.setStatus(3);
				detailsQo.setSeachStudent(true);
				pagination.setCondition(detailsQo);
				pagination.setPageNo(page);
				pagination = jobDetailsService.queryPagination(pagination);
				List<JobDetails> jobDetailsList=(List<JobDetails>)pagination.getList();
				SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				//				for(PostJob job:jobs){
				//					job.setCreateDate(format1.format(job.getCreatTime()));
				//					JobDetailsQo qo=new JobDetailsQo();
				//					qo.setPostJobId(job.getId());
				//					qo.setStatus(1);
				//					Integer submitSum=jobDetailsService.queryCount(qo);
				//					job.setSubmitNum(submitSum);
				//				}
				SysProperties sysProperties=SysProperties.getInstance();
				String operation=sysProperties.get("imagePath");
				for(JobDetails details:jobDetailsList){
					if(details.getNotchangedURL()!=null){
						details.setNotchangedURL(operation+"/operation/"+details.getNotchangedURL());
					}
					if(details.getChangepaintingURL()!=null){
						details.setChangepaintingURL(operation+"/operation/"+details.getChangepaintingURL());
					}
				}
				resMap.put("code", "200");
				resMap.put("message", pagination);
				return JSONObject.fromObject(resMap).toString();
			}
		}
	}
	//未交作业列表
	@ResponseBody
	@RequestMapping(value = "/noSubmitList")
	public String noSubmitList(HttpServletRequest request, Model model,@RequestParam(value="token", required=true) String token,@RequestParam(value="page", defaultValue="1")Integer page,@RequestParam(value="jobId", required=true) String jobId){
		Map<String, Object> resMap=new HashMap<String, Object>();
		if(StringUtils.isEmpty(token)) {
			resMap.put("code", "500");
			resMap.put("message", "未登录!");
			return JSONObject.fromObject(resMap).toString();
		}
		AdminSessionQo adminSessionQo=new AdminSessionQo();
		adminSessionQo.setToken(token);
		AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
		if(adminSession==null) {
			resMap.put("code", "406");
			resMap.put("message", "不存在此账号！");
			return JSONObject.fromObject(resMap).toString();
		}else {
			adminSession.getLoginName();
			UserQo userQo=new UserQo();
			userQo.setUserName(adminSession.getLoginName());
			User user= userService.queryUnique(userQo);
			if(user==null) {

				resMap.put("code", "406");
				resMap.put("message", "未登录!");
				return JSONObject.fromObject(resMap).toString();
			}else {
				Pagination pagination = new Pagination();
				pagination.setPageSize(10);
				JobDetailsQo detailsQo=new JobDetailsQo();
				detailsQo.setPostJobId(Long.parseLong(jobId));
				detailsQo.setStatus(2);
				detailsQo.setSeachStudent(true);
				pagination.setCondition(detailsQo);
				pagination.setPageNo(page);
				pagination = jobDetailsService.queryPagination(pagination);
				List<JobDetails> jobDetailsList=(List<JobDetails>)pagination.getList();
				SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				//				for(PostJob job:jobs){
				//					job.setCreateDate(format1.format(job.getCreatTime()));
				//					JobDetailsQo qo=new JobDetailsQo();
				//					qo.setPostJobId(job.getId());
				//					qo.setStatus(1);
				//					Integer submitSum=jobDetailsService.queryCount(qo);
				//					job.setSubmitNum(submitSum);
				//				}
				resMap.put("code", "200");
				resMap.put("message", pagination);
				return JSONObject.fromObject(resMap).toString();
			}
		}
	}
	/**
	 * 上交作业
	 * @param file
	 * @param token
	 * @param jobDetailsId
	 * @return
	 */
	@RequestMapping("/jobDetails_post")
	@ResponseBody
	public String jobDetails_Post(@RequestParam("file") MultipartFile file,@RequestParam(value="token", required=true)String token,String postJobId) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		AdminSessionQo adminSessionQo=new AdminSessionQo();
		adminSessionQo.setToken(token);
		AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
		if(adminSession==null) {
			resMap.put("code", "406");
			resMap.put("message", "不存在此账号！");
			return JSONObject.fromObject(resMap).toString();
		}else {
			UserQo userQo=new UserQo();
			userQo.setUserName(adminSession.getLoginName());
			User user= userService.queryUnique(userQo);
			if(user==null) {
				resMap.put("code", "406");
				resMap.put("message", "未登录!");
				return JSONObject.fromObject(resMap).toString();
			}else {
				String originalFilename = file.getOriginalFilename();
				//文件名使用当前时间
				String date = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
				// 获取上传图片的扩展名(jpg/png/...)
				String extension = FilenameUtils.getExtension(originalFilename);
				String[] regular = { "JPG", "GIF","PNG","JPEG" ,"BMP"};
				boolean flag = false;
				// 判断文件格式是否合法
				for (String str : regular) {
					if (extension.equalsIgnoreCase(str)) {
						flag = true;
					}
				}
				if (!flag) {
					resMap.put("code", "500");
					resMap.put("message", "上传图片格式不正确！");
					return JSONObject.fromObject(resMap).toString();
				}

				SysProperties sysProperties=SysProperties.getInstance();
				String operation=sysProperties.get("operation");
				// 图片上传的相对路径（因为相对路径放到页面上就可以显示图片）

				String path =operation+user.getId()+"/" + date + "." + extension;
				//图片上传的绝对路径

				File dir = new File(path);
				if (!dir.exists()) {
					dir.mkdirs();
				}
				// 上传图片
				try {
					file.transferTo(new File(path));
					int width=ImageTools.getImgWidth(dir);
					int height=ImageTools.getImgHeight(dir);
					StudentQo qo=new StudentQo();
					qo.setSeachStudent(true);
					qo.setUserid(user.getId());
					Student student= studentservice.queryUnique(qo);
					JobDetailsQo jobDetailsQo=new JobDetailsQo();
					jobDetailsQo.setSeachPostJob(true);
					jobDetailsQo.setSeachStudent(true);
					jobDetailsQo.setPostJobId(Long.parseLong(postJobId));
					jobDetailsQo.setStudentId(student.getId());
					JobDetails details= detailsService.queryUnique(jobDetailsQo);
					Date date2=new Date();
					if(details.getPostJob().getEndTime().before(date2) && details.getPostJob().getStartTime().after(date2)) {
						resMap.put("code", "500");
						resMap.put("message", "你只能在规定的开始时间和结束时间交作业!");
						return JSONObject.fromObject(resMap).toString();
					}
					details.setNotchangedURL(user.getId()+"/"+ date + "." + extension);
					details.setReleasetime(new Date());
					details.setWidth(width);
					details.setHeight(height);
					details.setStatus(1);
					JobDetails details2= detailsService.update(details);
					if(details2!=null) {
						resMap.put("code", "200");
						resMap.put("message", "成功");
						return JSONObject.fromObject(resMap).toString();
					}
					resMap.put("code", "500");
					resMap.put("message", "操作失败");
					return JSONObject.fromObject(resMap).toString();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		resMap.put("code", "406");
		resMap.put("message", "未登录!");
		return JSONObject.fromObject(resMap).toString();

	}
	/**
	 * 老师批改作业
	 * @param file
	 * @param token
	 * @param jobDetailsId
	 * @return
	 */
	@RequestMapping("/jobDetails_Correction")
	@ResponseBody
	public String jobDetails_Correction(@RequestParam("file") MultipartFile file,@RequestParam(value="token", required=true)String token,String jobDetailsId,String sccore,String comment) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession==null) {
				resMap.put("code", "406");
				resMap.put("message", "不存在此账号！");
				return JSONObject.fromObject(resMap).toString();
			}else {
				UserQo userQo=new UserQo();
				userQo.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo);
				if(user==null) {
					resMap.put("code", "406");
					resMap.put("message", "未登录!");
					return JSONObject.fromObject(resMap).toString();
				}else {
					if(file.getSize()<=0) {
						resMap.put("code", "500");
						resMap.put("message", "图片不能为空！");
						return JSONObject.fromObject(resMap).toString();
					}
					JobDetailsQo detailsQo=new JobDetailsQo();
					detailsQo.setSeachPostJob(true);
					detailsQo.setSeachStudent(true);
					detailsQo.setJobDetailsId(Long.parseLong(jobDetailsId));
					JobDetails details=detailsService.queryUnique(detailsQo);
					if(details.getStatus()==3) {
						resMap.put("code", "500");
						resMap.put("message", "已经批改！");
						return JSONObject.fromObject(resMap).toString();
					}
					//获取图片原始文件名
					String originalFilename = file.getOriginalFilename();
					//文件名使用当前时间
					String date = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
					// 获取上传图片的扩展名(jpg/png/...)
					String extension = FilenameUtils.getExtension(originalFilename);
					String[] regular = { "JPG", "GIF","PNG","JPEG" ,"BMP"};
					boolean flag = false;
					// 判断文件格式是否合法
					for (String str : regular) {
						if (extension.equalsIgnoreCase(str)) {
							flag = true;
						}
					}
					if (!flag) {
						resMap.put("code", "500");
						resMap.put("message", "上传图片格式不正确！");
						return JSONObject.fromObject(resMap).toString();
					}

					SysProperties sysProperties=SysProperties.getInstance();
					String operation=sysProperties.get("operation");
					// 图片上传的相对路径（因为相对路径放到页面上就可以显示图片）

					String path =operation+user.getId()+"/" + date + "." + extension;
					//图片上传的绝对路径

					File dir = new File(path);
					if (!dir.exists()) {
						dir.mkdirs();
					}
					// 上传图片
					try {
						file.transferTo(new File(path));
						details.setSccore(Integer.parseInt(sccore));
						details.setComment(comment);
						details.setReviewtime(new Date());
						details.setStatus(3);
						details.setChangepaintingURL(user.getId()+"/" + date + "." + extension);
						JobDetails details2= detailsService.update(details);
						if(details2!=null) {
							resMap.put("code", "200");
							resMap.put("message", "成功");
							return JSONObject.fromObject(resMap).toString();
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						resMap.put("code", "500");
						resMap.put("message", "操作失败");
						return JSONObject.fromObject(resMap).toString();
					}
				}
			}
			resMap.put("code", "500");
			resMap.put("message", "操作失败");
			return JSONObject.fromObject(resMap).toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "系统异常！");
			return JSONObject.fromObject(resMap).toString();
		}

	}
	/**
	 * 作业完成情况
	 * @param studentId
	 * @return
	 */
	@RequestMapping("/jobDetails_JobCount")
	@ResponseBody
	public String jobDetails_JobCount(@RequestParam(value="studentId", required=true)String studentId,@RequestParam(value="token", required=true) String token) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession==null) {
				resMap.put("code", "406");
				resMap.put("message", "不存在此账号！");
				return JSONObject.fromObject(resMap).toString();
			}else {
				UserQo userQo=new UserQo();
				userQo.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo);
				if(user==null) {
					resMap.put("code", "406");
					resMap.put("message", "未登录!");
					return JSONObject.fromObject(resMap).toString();
				}else {
					JobDetailsQo detailsQo=new JobDetailsQo();
					detailsQo.setSeachPostJob(true);
					detailsQo.setSeachStudent(true);
					detailsQo.setStudentId(Long.parseLong(studentId));
					JobDetails details=detailsService.queryUnique(detailsQo);
					JobDetailsCommand command=new JobDetailsCommand();
					SysProperties sysProperties=SysProperties.getInstance();
					String imagePath=sysProperties.get("imagePath");
					if(details==null){
						Student student=studentservice.get(Long.parseLong(studentId));
						command.setAvatar(imagePath+"/picture/"+student.getAvatar());
						command.setJobCount(0);
						command.setNotPaidCount(0);
						command.setStudentName(student.getNickname());
						resMap.put("code", "200");
						resMap.put("message", command);
						return JSONObject.fromObject(resMap).toString();
					}
					int count=detailsService.queryCount(detailsQo);
					detailsQo.setStatus(2);
					int notPaidCount =detailsService.queryCount(detailsQo);
					

					command.setAvatar(imagePath+"/picture/"+details.getStudent().getAvatar());
					command.setJobCount(count);
					command.setNotPaidCount(notPaidCount);
					command.setStudentName(details.getStudent().getNickname());
					resMap.put("code", "200");
					resMap.put("message", command);
					return JSONObject.fromObject(resMap).toString();
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "系统异常！");
			return JSONObject.fromObject(resMap).toString();
		}
	}
	/**
	 * 作业列表
	 * @param page
	 * @param studentId
	 * @return
	 */
	@RequestMapping("/jobDetails_Page")
	@ResponseBody
	public String jobDetails_Page(@RequestParam(value="page", defaultValue="1")Integer page,@RequestParam(value="studentId", required=true)String studentId,@RequestParam(value="token", required=true) String token) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		AdminSessionQo adminSessionQo=new AdminSessionQo();
		adminSessionQo.setToken(token);
		AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
		if(adminSession==null) {
			resMap.put("code", "406");
			resMap.put("message", "不存在此账号！");
			return JSONObject.fromObject(resMap).toString();
		}else {
			UserQo userQo=new UserQo();
			userQo.setUserName(adminSession.getLoginName());
			User user= userService.queryUnique(userQo);
			if(user==null) {
				resMap.put("code", "406");
				resMap.put("message", "未登录!");
				return JSONObject.fromObject(resMap).toString();
			}else {
				JobDetailsQo detailsQo=new JobDetailsQo();
				detailsQo.setSeachPostJob(true);
				detailsQo.setSeachStudent(true);
				detailsQo.setStudentId(Long.parseLong(studentId));
				Pagination pagination = new Pagination();
				pagination.setPageSize(10);
				pagination.setPageNo(page);
				pagination.setCondition(detailsQo);
				pagination = detailsService.queryPagination(pagination);
				resMap.put("code", "200");
				resMap.put("message", pagination);
				return JSONObject.fromObject(resMap).toString();
			}
		}
	}
	
	/**
	 * 我的已交作业列表
	 * @param page
	 * @param studentId
	 * @return
	 */
	@RequestMapping("/studentJobDetails_Page")
	@ResponseBody
	public String studentJobDetails_Page(@RequestParam(value="page", defaultValue="1")Integer page,@RequestParam(value="token", required=true) String token) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		AdminSessionQo adminSessionQo=new AdminSessionQo();
		adminSessionQo.setToken(token);
		AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
		if(adminSession==null) {
			resMap.put("code", "406");
			resMap.put("message", "不存在此账号！");
			return JSONObject.fromObject(resMap).toString();
		}else {
			UserQo userQo=new UserQo();
			userQo.setUserName(adminSession.getLoginName());
			User user= userService.queryUnique(userQo);
			if(user==null) {
				resMap.put("code", "406");
				resMap.put("message", "未登录!");
				return JSONObject.fromObject(resMap).toString();
			}else {
				StudentQo studentQo=new StudentQo();
				studentQo.setUserid(user.getId());
				Student student= studentservice.queryUnique(studentQo);
				if(student==null) {
					resMap.put("code", "406");
					resMap.put("message", "未登录!");
					return JSONObject.fromObject(resMap).toString();
				}
				JobDetailsQo detailsQo=new JobDetailsQo();
				detailsQo.setSeachPostJob(true);
				detailsQo.setStatus(1);
				detailsQo.setSeachStudent(true);
				detailsQo.setStudentId(student.getId());
				Pagination pagination = new Pagination();
				pagination.setPageSize(10);
				pagination.setPageNo(page);
				pagination.setCondition(detailsQo);
				pagination = detailsService.queryPagination(pagination);
				SysProperties sysProperties=SysProperties.getInstance();
				String operation=sysProperties.get("imagePath");

				List<JobDetails> jobDetailsList=(List<JobDetails>)pagination.getList();
				for(JobDetails details:jobDetailsList){
					if(details.getNotchangedURL()!=null){
						details.setNotchangedURL(operation+"/operation/"+details.getNotchangedURL());
					}
					if(details.getChangepaintingURL()!=null){
						details.setChangepaintingURL(operation+"/operation/"+details.getChangepaintingURL());
					}
				}
				resMap.put("code", "200");
				resMap.put("message", pagination);
				return JSONObject.fromObject(resMap).toString();
			}
		}
	}
	/**
	 * 我的已批改作业列表
	 * @param page
	 * @param studentId
	 * @return
	 */
	@RequestMapping("/studentCorrectJobDetails_Page")
	@ResponseBody
	public String studentCorrectJobDetails_Page(@RequestParam(value="page", defaultValue="1")Integer page,@RequestParam(value="token", required=true) String token) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		AdminSessionQo adminSessionQo=new AdminSessionQo();
		adminSessionQo.setToken(token);
		AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
		if(adminSession==null) {
			resMap.put("code", "406");
			resMap.put("message", "不存在此账号！");
			return JSONObject.fromObject(resMap).toString();
		}else {
			UserQo userQo=new UserQo();
			userQo.setUserName(adminSession.getLoginName());
			User user= userService.queryUnique(userQo);
			if(user==null) {
				resMap.put("code", "406");
				resMap.put("message", "未登录!");
				return JSONObject.fromObject(resMap).toString();
			}else {
				StudentQo studentQo=new StudentQo();
				studentQo.setUserid(user.getId());
				Student student= studentservice.queryUnique(studentQo);
				if(student==null) {
					resMap.put("code", "406");
					resMap.put("message", "未登录!");
					return JSONObject.fromObject(resMap).toString();
				}
				JobDetailsQo detailsQo=new JobDetailsQo();
				detailsQo.setSeachPostJob(true);
				detailsQo.setStatus(3);
				detailsQo.setSeachStudent(true);
				detailsQo.setStudentId(student.getId());
				Pagination pagination = new Pagination();
				pagination.setPageSize(10);
				pagination.setPageNo(page);
				pagination.setCondition(detailsQo);
				pagination = detailsService.queryPagination(pagination);
				SysProperties sysProperties=SysProperties.getInstance();
				String operation=sysProperties.get("imagePath");

				List<JobDetails> jobDetailsList=(List<JobDetails>)pagination.getList();
				for(JobDetails details:jobDetailsList){
					if(details.getNotchangedURL()!=null){
						details.setNotchangedURL(operation+"/operation/"+details.getNotchangedURL());
					}
					if(details.getChangepaintingURL()!=null){
						details.setChangepaintingURL(operation+"/operation/"+details.getChangepaintingURL());
					}
				}
				resMap.put("code", "200");
				resMap.put("message", pagination);
				return JSONObject.fromObject(resMap).toString();
			}
		}
	}
	/**
	 * 我的未交作业列表
	 * @param page
	 * @param studentId
	 * @return
	 */
	@RequestMapping("/studentNoJobDetails_Page")
	@ResponseBody
	public String studentNoJobDetails_Page(@RequestParam(value="page", defaultValue="1")Integer page,@RequestParam(value="token", required=true) String token) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		AdminSessionQo adminSessionQo=new AdminSessionQo();
		adminSessionQo.setToken(token);
		AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
		if(adminSession==null) {
			resMap.put("code", "406");
			resMap.put("message", "不存在此账号！");
			return JSONObject.fromObject(resMap).toString();
		}else {
			UserQo userQo=new UserQo();
			userQo.setUserName(adminSession.getLoginName());
			User user= userService.queryUnique(userQo);
			if(user==null) {
				resMap.put("code", "406");
				resMap.put("message", "未登录!");
				return JSONObject.fromObject(resMap).toString();
			}else {
				StudentQo studentQo=new StudentQo();
				studentQo.setUserid(user.getId());
				Student student= studentservice.queryUnique(studentQo);
				if(student==null) {
					resMap.put("code", "406");
					resMap.put("message", "未登录!");
					return JSONObject.fromObject(resMap).toString();
				}
				JobDetailsQo detailsQo=new JobDetailsQo();
				detailsQo.setSeachTypeOne(true);
				detailsQo.setSeachPostJob(true);
				detailsQo.setStatus(2);
				detailsQo.setSeachStudent(true);
				detailsQo.setStudentId(student.getId());
				Pagination pagination = new Pagination();
				pagination.setPageSize(10);
				pagination.setPageNo(page);
				pagination.setCondition(detailsQo);
				pagination = detailsService.queryPagination(pagination);
				
				resMap.put("code", "200");
				resMap.put("message", pagination);
				return JSONObject.fromObject(resMap).toString();
			}
		}
	}
	@RequestMapping("/jobDetails_modify")
	@ResponseBody
	public String jobDetails_Modify(@RequestParam(value="jobDetailsId", required=true)String jobDetailsId,@RequestParam(value="token", required=true) String token) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			AdminSessionQo adminSessionQo=new AdminSessionQo();
			adminSessionQo.setToken(token);
			AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
			if(adminSession==null) {
				resMap.put("code", "406");
				resMap.put("message", "不存在此账号！");
				return JSONObject.fromObject(resMap).toString();
			}else {
				UserQo userQo=new UserQo();
				userQo.setUserName(adminSession.getLoginName());
				User user= userService.queryUnique(userQo);
				if(user==null) {
					resMap.put("code", "406");
					resMap.put("message", "未登录!");
					return JSONObject.fromObject(resMap).toString();
				}else {
					JobDetailsQo detailsQo=new JobDetailsQo();
					detailsQo.setSeachPostJob(true);
					detailsQo.setSeachStudent(true);
					detailsQo.setJobDetailsId(Long.parseLong(jobDetailsId));
					JobDetails details=detailsService.queryUnique(detailsQo);
					details.setUnpaidReason(3);
					JobDetails details1=detailsService.update(details);
					if(details1!=null) {
						resMap.put("code", "200");
						resMap.put("message", "成功！");
						return JSONObject.fromObject(resMap).toString();
					}
					resMap.put("code", "500");
					resMap.put("message", "失败！");
					return JSONObject.fromObject(resMap).toString();
					
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "系统异常！");
			return JSONObject.fromObject(resMap).toString();
		}
		
	}
	/**
	 * 作业详情
	 * @param jobDetails
	 * @param token
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/jobDetails")
	public String jobDetails( @RequestParam(value="jobDetailsId", required=true) String jobDetailsId,@RequestParam(value="token", required=true) String token) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		AdminSessionQo adminSessionQo1=new AdminSessionQo();
		adminSessionQo1.setToken(token);
		AdminSession adminSession1=adminSessionService.queryUnique(adminSessionQo1);
		if(adminSession1==null) {
			resMap.put("code", "406");
			resMap.put("message", "不存在此账号！");
			return JSONObject.fromObject(resMap).toString();
		}else {
			UserQo userQo=new UserQo();
			userQo.setUserName(adminSession1.getLoginName());
			User user= userService.queryUnique(userQo);
			if(user==null) {
				resMap.put("code", "406");
				resMap.put("message", "未登录!");
				return JSONObject.fromObject(resMap).toString();
			}else {
				JobDetails details=detailsService.get(Long.parseLong(jobDetailsId));
				
				SysProperties sysProperties=SysProperties.getInstance();
				String operation=sysProperties.get("imagePath");
				if(details.getNotchangedURL()!=null){
					details.setNotchangedURL(operation+"/operation/"+details.getNotchangedURL());
				}
				resMap.put("code", "200");
				resMap.put("message", details);
				
				return JSONObject.fromObject(resMap).toString();
			}
		}
		
	}
}
